package com.xqh.study.springtxjpa.web;

import com.xqh.study.springtxjpa.dao.CustomerRepository;
import com.xqh.study.springtxjpa.domain.Customer;
import com.xqh.study.springtxjpa.service.CustomerServiceInAnnotation;
import com.xqh.study.springtxjpa.service.CustomerServiceInCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CustomerResource
 * @Description TODO
 * @Author xuqianghui
 * @Date 2019/12/30 21:41
 * @Version 1.0
 */
@RequestMapping("/api/customer")
@RestController
public class CustomerResource {

    @Autowired
    private CustomerServiceInAnnotation serviceInAnnotation;

    @Autowired
    private CustomerServiceInCode serviceInCode;

    @Autowired
    private CustomerRepository customerRepository;

    @PostMapping(value = "code")
    @Transactional
    public Customer createInCode(@RequestBody Customer customer){
        return serviceInCode.create(customer);
    }

    @PostMapping(value = "annotation")
    public Customer createInAnnotation(@RequestBody Customer customer){
        return serviceInAnnotation.create(customer);
    }

    @RequestMapping(value = "list")
    public List<Customer> list(){
        return customerRepository.findAll();
    }




}
